package com.yuanluludev.lib_webview.web

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import com.yuanluludev.lib_webview.R
import kotlinx.android.synthetic.main.activity_web.*

/**
 * yuanLulu
 * 用于在activity中加载web页面
 */
class WebActivity : BaseAgentWebActivity() {

    companion object {
        private const val WEB_URL = "web_url"

        @JvmStatic
        fun jump(context: Context, webUrl: String) {
            context.startActivity(Intent(context, WebActivity::class.java).apply {
                putExtra(WEB_URL, webUrl)
            })
        }
    }

    private var webUrl: String = ""

    override fun getLayoutResId() = R.layout.activity_web

    override fun getAgentWebParent(): ViewGroup = rlContainer

    override fun initEvents() {
        //回退事件
        ivGoBack?.setOnClickListener {
            if (mAgentWeb != null && mAgentWeb?.webCreator?.webView?.canGoBack() == true) {
                mAgentWeb?.webCreator?.webView?.goBack()
            } else {
                finish()
            }
        }

        //如果是视频或者图片 隐藏退出按钮
        val webUrl = (intent?.getStringExtra(WEB_URL) ?: "")
        if(webUrl.endsWith("mp4")||webUrl.endsWith("jpg")||webUrl.endsWith("jpeg")||webUrl.endsWith("png")){
            ivBack?.visibility = View.GONE
        }
    }

    override fun getUrl(): String {
        webUrl = intent?.getStringExtra(WEB_URL) ?: ""
        return webUrl
    }

    override fun setTitle(view: WebView?, title: String?) {
        super.setTitle(view, title)
        tvTitle?.text = title ?: ""
    }

    override fun getWebView(): WebView? {
        return super.getWebView()
    }

}